package com.ruoyi.web.config;

import com.ruoyi.common.enums.BaseInteger;
import com.ruoyi.common.enums.BaseString;
import com.ruoyi.common.handler.EnumTypeHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @PostConstruct
    public void init() {
        //DAO层解析枚举,
        TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
        //对枚举的处理，TODO 未处理xml中的typeHandler
        typeHandlerRegistry.setDefaultEnumTypeHandler(EnumTypeHandler.class);
    }
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new IntegerToEnumConverterFactory());
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }

    private static class IntegerToEnumConverterFactory implements ConverterFactory<String, BaseInteger> {

        @Override
        public <T extends BaseInteger> Converter<String, T> getConverter(Class<T> targetType) {
            return new StringToCodeEnumConverter<>(targetType);
        }

        private static class StringToCodeEnumConverter<T extends BaseInteger> implements Converter<String, T> {

            private final Class<T> enumType;

            public StringToCodeEnumConverter(Class<T> enumType) {
                this.enumType = enumType;
            }

            @Override
            public T convert(String source) {
                if (source.isEmpty()) {
                    return null;
                }
                Integer code = Integer.valueOf(source);
                return BaseInteger.fromCode(enumType, code);
            }
        }
    }

    private static class StringToEnumConverterFactory implements ConverterFactory<String, BaseString> {

        @Override
        public <T extends BaseString> Converter<String, T> getConverter(Class<T> targetType) {
            return new StringToCodeEnumConverter<>(targetType);
        }

        private static class StringToCodeEnumConverter<T extends BaseString> implements Converter<String, T> {

            private final Class<T> enumType;

            public StringToCodeEnumConverter(Class<T> enumType) {
                this.enumType = enumType;
            }

            @Override
            public T convert(String source) {
                if (source.isEmpty()) {
                    return null;
                }
                return BaseString.fromCode(enumType, source);
            }
        }
    }
}